package book.book.controller;

import book.book.bean.Books;
import book.book.bean.Borrowinfo;
import book.book.bean.User;
import book.book.dao.BorrowinfoDao;
import book.book.dao.BorrowinfoDaoImpl;
import book.book.dao.UserDao;
import book.book.dao.UserDaoImpl;
import book.book.service.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class main1 {
    public static void main(String[] args) {
        BooksService booksService = new BooksServiceImpl();
        BorrowinfoDao borrowinfoDao = new BorrowinfoDaoImpl();
        BorrowinfoServiceImpl borrowinfoService = new BorrowinfoServiceImpl();
        UserService userService = new UserServiceImpl();
        UserDao userDao = new UserDaoImpl();
        System.out.println("-----------------------------------------");
        System.out.println("------------欢迎使用图书管理系统------------");
        System.out.println("-----------------------------------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        //登录
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User login = userService.login(user);
        if (login == null) {
            System.out.println("用户名或密码错误！");
        } else {
            System.out.println("登录成功！");
            System.out.println(login);
        }
        boolean running = true;
while(running) {
    System.out.println("请输入数字去选择角色: 1是普通用户 2是管理员 3是退出");
    int i = sc.nextInt();
    if(i == 3){
        running = false;
        System.out.println("欢迎下次使用！");
    }
    if (i == 1) {
        //普通用户的操作
        System.out.println("查看所有书籍输入1:");
        System.out.println("借书请输入2:");
        System.out.println("查看借阅记录输入3:");
        int u1 = sc.nextInt();
        switch (u1) {
            case 1:
                // 查询所有书籍,
                List<Books> books = booksService.selectAllBooks();
                books.iterator().forEachRemaining(System.out::println);
                break;
            case 2://借书
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedDate = LocalDateTime.now().format(formatter);
                System.out.println("请输入要借的书籍id:");
                String bookid = sc.next();
                System.out.println("请输入你的手机号:");
                String phone = sc.next();
                Borrowinfo borrowinfo = new Borrowinfo();
                borrowinfo.setBorrowid(UUID.randomUUID().toString().replaceAll("-", ""));
                borrowinfo.setBookid(bookid);
                borrowinfo.setBorrower("花飞");
                borrowinfo.setBorrowtime(formattedDate);
                borrowinfo.setRetruntime(null);
                borrowinfo.setIsreturn(0);
                borrowinfo.setPhone(phone);
                boolean b = borrowinfoService.addBorrowinfo(borrowinfo);
                if (b) {
                    System.out.println("借阅成功！");
                    //借阅成功则减少书籍数量
                    borrowinfoService.updateBookCount(bookid);
                } else {
                    System.out.println("借阅失败！");
                }
            case 3:
                //查看借阅记录
                System.out.println("请输入你的用户名:");
                String username1 = sc.next();
                List<Borrowinfo> borrowinfos = borrowinfoService.selectBinfoByUser(username1);
                borrowinfos.iterator().forEachRemaining(System.out::println);
                break;
        }////用户
    } else if (i == 2) {
        //管理员的操作
        System.out.println("增加图书请输1");
        System.out.println("修改图书请输2");
        System.out.println("删除图书请输3");
        System.out.println("查询图书信息请输4");
        System.out.println("根据书名模糊查询输入5");
        System.out.println("还书请输入6:");
        System.out.println("查看所有用户信息输入7:");
        int adminNumber = sc.nextInt();
        switch (adminNumber) {
            case 1:
                //调用增加图书方法，service中的
                String bookid = UUID.randomUUID().toString().replaceAll("-", "");
                System.out.println("请输入要添加的书籍名称:");
                String bookName = sc.next();
                System.out.println("请输入要添加的书籍出版社:");
                String publisher = sc.next();
                System.out.println("请输入要添加的书籍作者:");
                String author = sc.next();
                System.out.println("请输入要添加的书籍类型:");
                String bookType = sc.next();
                System.out.println("请输入要添加的书籍数量:");
                int remain = sc.nextInt();
                Books books = new Books(bookid, bookName, publisher, author, bookType, remain);
                boolean b = booksService.addBook(books);
                System.out.println(b ? "添加成功" : "添加失败");
                break;
            case 2:
                System.out.println("请输入要修改的书籍id: ");
                String bookid1 = sc.next();
                System.out.println("请输入要修改的书籍新名称:");
                String bookName1 = sc.next();
                System.out.println("请输入要修改的书籍新出版社:");
                String publisher1 = sc.next();
                System.out.println("请输入要修改的书籍新作者:");
                String author1 = sc.next();
                System.out.println("请输入要修改的书籍新类型:");
                String bookType1 = sc.next();
                System.out.println("请输入要添修改的书籍新数量:");
                int remain1 = sc.nextInt();
                Books books1 = new Books(bookid1, bookName1, publisher1, author1, bookType1, remain1);
                boolean b1 = booksService.updateBookInfo(books1);
                System.out.println(b1 ? "修改成功" : "修改失败");
                //调用修改图书方法，service中的
                break;
            case 3:
                //调用删除图书方法
                // 删除书籍,需要加入自己传值
                System.out.println("请输入要删除的书籍名: ");
                String bookname = sc.next();
                System.out.println("请输入要删除的书籍作者: ");
                String author2 = sc.next();
                boolean flag1 = booksService.delBookById(bookname, author2);
                System.out.println(flag1 ? "删除成功" : "删除失败");
                break;
            case 4:
                List<Books> bookh1 = booksService.selectAllBooks();
                bookh1.iterator().forEachRemaining(System.out::println);
                // 查询书籍信息,需要加入自己传值根据id查询
                // Books books = booksService.selectBooksByname("葫芦娃", "罗玲");
                // System.out.println(books);
                break;
            case 5:
                //实现模糊查询
                System.out.println("请输入要查询的书籍名:");
                String booknameh1 = sc.next();

                List<Books> booksList = booksService.queryBookLikeName(booknameh1);
                for (Books booksh2 : booksList) {
                    System.out.println(booksh2.toString());
                }
                break;
            case 6:
                //还书
                System.out.println("请输入要还的书籍id:");
                String bookid2 = sc.next();
                System.out.println("请输入要还的书籍借阅人:");
                String borrower = sc.next();
                borrowinfoService.updateBookCountBack(bookid2, borrower);
                break;

            case 7:
                //查看所有用户信息
                List<User> users = userService.getAllUsers();
                users.iterator().forEachRemaining(System.out::println);
                break;
        }
    }
}
    }
}

